﻿//document.cookie = "tallyid=;path=/";


//删除事件
function delData(obj, isab, id) {
    if (isab == "a") {
        $.ajax({
            type: "post",

            url: "/ashx/Tally.ashx",

            data: { afterid: id },

            success: function (msg) {
                alert('删除成功');
            }
        });
    }
    else if (isab == "b") {
        $.ajax({
            type: "post",

            url: "/ashx/Tally.ashx",

            data: { beforeid: id },

            success: function (msg) {
                alert('删除成功');
            }
        });
    }
    $(obj).parent().parent().remove();
}

//编辑事件
function editData(obj, isab, id) {
    $(obj).parent().parent().parent().children("tr").css("color", "black");
    $(obj).parent().parent().parent().children("tr").attr("id", false);
    if (isab == "a") {
        $("#txtgc_1").val($(obj).parent().parent().children("td").eq(1).html());
        $("#txtgn_1").val($(obj).parent().parent().children("td").eq(2).html());
        $("#txtnt_1").val($(obj).parent().parent().children("td").eq(3).html());
        $("#txtboxcount_1").val($(obj).parent().parent().children("td").eq(4).html());
        $("#txtqty_1").val($(obj).parent().parent().children("td").eq(5).html());
        $("#txtrubc_1").val($(obj).parent().parent().children("td").eq(6).html());
        $("#txtrubt_1").val($(obj).parent().parent().children("td").eq(7).html());
        $("#txtldh_1").val($(obj).parent().parent().children("td").eq(8).html());
        $("#dropcon_1").val($(obj).parent().parent().children("td").eq(9).html());
        $("#txtboxno_1").val($(obj).parent().parent().children("td").eq(10).html());
        $("#txtremark_1").val($(obj).parent().parent().children("td").eq(11).html());
        $("#hidaid").val(id);
        $("#btnIn1").val("更新");
        $(obj).parent().parent().attr("id", "tr_aftid");
    }
    else if (isab == "b") {
        $("#txtgc_0").val($(obj).parent().parent().children("td").eq(1).html());
        $("#txtgn_0").val($(obj).parent().parent().children("td").eq(2).html());
        $("#txtnt_0").val($(obj).parent().parent().children("td").eq(3).html());
        $("#txtqty_0").val($(obj).parent().parent().children("td").eq(4).html());
        $("#txtrubc_0").val($(obj).parent().parent().children("td").eq(5).html());
        $("#txtrubt_0").val($(obj).parent().parent().children("td").eq(6).html());
        $("#txtldh_0").val($(obj).parent().parent().children("td").eq(7).html());
        $("#dropcon_0").val($(obj).parent().parent().children("td").eq(8).html());
        $("#txtboxno_0").val($(obj).parent().parent().children("td").eq(9).html());
        $("#txtremark_0").val($(obj).parent().parent().children("td").eq(10).html());
        $("#hidbid").val(id);
        $("#btnIn0").val("更新");
        $(obj).parent().parent().attr("id", "tr_befid");
    }
    //$(obj).parent().parent().style.color = "Red";
    $(obj).parent().parent().css("color", "red");
}

//计算理货处理前的剩余重量
function comWeihtSurplus_b() {
    var totalweight = parseFloat($(".lblsumWeight").html());

    //处理前
    var tbweight = 0.00;
    $(".td_tb_nt").each(function () {
        tbweight = tbweight + parseFloat($(this).html());
    });
    $(".css_nt_tb").html(totalweight - tbweight);

  }

//计算理货处理后的剩余重量
function comWeihtSurplus_a() {
    var totalweight = parseFloat($(".lblsumWeight").html());

    //处理后
    var taweight = 0.0;
    $(".td_ta_nt").each(function () {
        taweight = taweight + parseFloat($(this).html());
    });
    $(".css_nt_ta").html(totalweight-taweight);
 }


 $(document).ready(function () {
      $("tr:even").addClass("even");

     $(".date1").change(function () {
         $(".hidOperDate").val($(".date1").val());
     });

     $("#txtgc_0").autocomplete({
         source: function (request, response) {
             $.ajax({
                 url: "/ashx/Autocomplete.ashx",
                 data: { "goods": $("#txtgc_0").val(), "type": "DH" },
                 dataType: "json",
                 success: function (data) {
                     response($.map(data, function (item) {
                         return {
                             label: item.label + " " + item.value,
                             value: item.value,
                             lab: item.label
                         }
                     }));
                 },
                 error: function (result, status) {
                     alert('请输入正确的物品编码');
                 }
             })

         },
         select: function (event, ui) {
             $("#txtgc_0").val(ui.item.lab);
             $("#txtgn_0").val(ui.item.value);
             $("#hidtxtgc_0id").val(ui.item.goodsid);
             return false;
         },

         minLength: "1"
     });

     $("#txtgc_1").autocomplete({
         source: function (request, response) {
             $.ajax({
                 url: "/ashx/Autocomplete.ashx",
                 data: { "goods": $("#txtgc_1").val(), "type": "T" },
                 dataType: "json",
                 success: function (data) {
                     response($.map(data, function (item) {
                         return {
                             label: item.label + " " + item.value,
                             value: item.value,
                             lab: item.label
                         }
                     }));
                 },
                 error: function (result, status) {
                     alert('请输入正确的物品编码');
                 }
             })

         },
         select: function (event, ui) {
             $("#txtgc_1").val(ui.item.lab);
             $("#txtgn_1").val(ui.item.value);
             $("#hidtxtgc_1id").val(ui.item.goodsid);
             return false;
         },

         minLength: "1"
     });


     //处理前危废下拉选择  2013.06.06  RedPlum
     $("#txtrubc_0").autocomplete({
         source: function (request, response) {
             $.ajax({
                 url: "/ashx/Autocomplete.ashx",
                 data: { "goods": $("#txtrubc_0").val(), "type": "hw" },
                 dataType: "json",
                 success: function (data) {
                     response($.map(data, function (item) {
                         return {
                             label: item.label + " " + item.value,
                             value: item.value,
                             lab: item.label
                         }
                     }));
                 },
                 error: function (result, status) {
                     alert('请输入正确的危废编码');
                 }
             })

         },
         select: function (event, ui) {
             $("#txtrubc_0").val(ui.item.lab);
             $("#txtrubt_0").val(ui.item.value);
             $("#hidrubc_0id").val(ui.item.goodsid);
             return false;
         },

         minLength: "1"
     });


     $("#txtrubc_1").autocomplete({
         source: function (request, response) {
             $.ajax({
                 url: "/ashx/Autocomplete.ashx",
                 data: { "goods": $("#txtrubc_1").val(), "type": "hw" },
                 dataType: "json",
                 success: function (data) {
                     response($.map(data, function (item) {
                         return {
                             label: item.label + " " + item.value,
                             value: item.value,
                             lab: item.label
                         }
                     }));
                 },
                 error: function (result, status) {
                     alert('请输入正确的危废编码');
                 }
             })

         },
         select: function (event, ui) {
             $("#txtrubc_1").val(ui.item.lab);
             $("#txtrubt_1").val(ui.item.value);
             $("#hidrubc_1id").val(ui.item.goodsid);
             return false;
         },

         minLength: "1"
     });



     $.datepicker.regional['zh-CN'] = {
         clearText: '清除',
         clearStatus: '清除已选日期',
         closeText: '关闭',
         closeStatus: '不改变当前选择',
         prevText: '<上月',
         prevStatus: '显示上月',
         prevBigText: '<<',
         prevBigStatus: '显示上一年',
         nextText: '下月>',
         nextStatus: '显示下月',
         nextBigText: '>>',
         nextBigStatus: '显示下一年',
         currentText: '今天',
         currentStatus: '显示本月',
         monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
         monthNamesShort: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
         monthStatus: '选择月份',
         yearStatus: '选择年份',
         weekHeader: '周',
         weekStatus: '年内周次',
         dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
         dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
         dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
         dayStatus: '设置 DD 为一周起始',
         dateStatus: '选择 m月 d日, DD',
         dateFormat: 'yy-mm-dd',
         firstDay: 1,
         initStatus: '请选择日期',
         isRTL: false
     };
     $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
     $(".date1").datepicker({
         "dateFormat": "yy-mm-dd"
     });

     $(function () {
         // 处理前录入
         $("#btnIn0").click(function () {
             var strErr = "";
             strErr = checkInputOne();
             if (strErr != "") {
                 alert(strErr);
                 return;
             }
             //验证物品编码和废物编码是否合法
             checkInsertFirst();

         });

         // 处理后录入
         $("#btnIn1").click(function () {
             var strErr2 = "";
             strErr2 = checkInputTwo()
             if (strErr2 != "") {
                 alert(strErr2);
                 return;
             }
             checkInsertEnd();
             $("tr:even").addClass("even");
         });
     });


     //处理前插入数据
     function insertFirst() {
         var jston = {
             "befid": $("#hidbid").val(),
             "gcode": $("#txtgc_0").val(),
             "gname": $("#txtgn_0").val(),
             "nt": $("#txtnt_0").val(),
             "boxcount": $("#txtnt_0").val(),
             "qty": $("#txtqty_0").val(),
             "rubc": $("#txtrubc_0").val(),
             "rubt": $("#txtrubt_0").val(),
             "ldh": $("#txtldh_0").val(),
             "boxno": $("#txtboxno_0").val(),
             "remark": $("#txtremark_0").val(),
             "con": $("#dropcon_0").val(),
             //tally公共数据
             "tallyid": $(".hidtallyid").val(),
             "wrlno": "",
             "loadno": $("#EwtsConext_tdLoad").val(),
             "opdate": $(".date1").val(),
             "loadid": $(".hidloadid").val(),
             "loadnum": $(".spanload").html(),
             "outid": $(".hidoutid").val()

         };

         $.ajax({
             type: "post",
             url: "/ashx/Tally.ashx?isab=b",
             data: jston,
             success: function (msg) {
                 var obj = eval('(' + msg + ')');
                 var tr = "";
                 tr += "<td>" + ($('#tab_3 tr').length - 1) + "</td>";
                 tr += "<td>" + obj[0].goodscode + "</td>";
                 tr += "<td>" + obj[0].goodsName + "</td>";
                 tr += "<td class='td_tb_nt'>" + obj[0].netwegight + "</td>";
                 tr += "<td>" + obj[0].quantity + "</td>";
                 tr += "<td>" + obj[0].rubbishcode + "</td>";
                 tr += "<td>" + obj[0].rubbishname + "</td>";
                 tr += "<td>" + obj[0].kupono + "</td>";
                 tr += "<td>" + obj[0].classify + "</td>";
                 tr += "<td>" + obj[0].boxno + "</td>";
                 tr += "<td>" + obj[0].remark + "</td>";
                 tr += "<td>";
                 tr += " <a href=\"javascript:void(0)\" onclick=\"editData(this,'b',";
                 tr += obj[0].id;
                 tr += ")\">编辑</a>";
                 tr += "<a onclick=\"javascript:delData(this,'b',";
                 tr += obj[0].id + ")\">删除</a>";
                 tr += "</td>";
                 $(".hidtallyid").val(obj[1].id);
                 // document.cookie = "tallyid=" + obj[1].id + ";path=/";

                 $(".txttallyno").val(obj[1].tallyno);
                 if (obj[0] != null && $("#hidbid").val() != "0") {
                     $("#tr_befid").html(tr);
                     $("#hidbid").val("0");
                     $("#tr_befid").css("color", "black");
                 }
                 else {
                     tr = "<tr>" + tr + "</tr>";
                     $("#tab_3").append(tr);
                 }

                 comWeihtSurplus_b();   //余下重量更新
                 $("#txtgc_0").val("");
                 $("#txtgn_0").val("");
                 $("#txtnt_0").val("");
                 $("#txtqty_0").val("");
                 $("#txtrubc_0").val("");
                 $("#txtrubt_0").val("");
                 $("#txtldh_0").val("");
                 $("#dropcon_0").val("");
                 $("#txtboxno_0").val("");
                 $("#txtremark_0").val("");
                 $("tr:even").addClass("even");
                 //                $("tr").css("background", "#f6f6f6");
                 //                $("tr:even").css("background", "#e9e9e9");
                 $("#btnIn0").val("插入");
             }
         });
     }

     //处理后插入数据
     function InsertEnd() {
         var jston = {
             "aftid": $("#hidaid").val(),
             "gcode": $("#txtgc_1").val(),
             "gname": $("#txtgn_1").val(),
             "nt": $("#txtnt_1").val(),
             "boxcount": $("#txtboxcount_1").val(),
             "qty": $("#txtqty_1").val(),
             "rubc": $("#txtrubc_1").val(),
             "rubt": $("#txtrubt_1").val(),
             "ldh": $("#txtldh_1").val(),
             "boxno": $("#txtboxno_1").val(),
             "remark": $("#txtremark_1").val(),
             "con": $("#dropcon_1").val(),

             //tally公共数据
             "tallyid": $(".hidtallyid").val(),
             "wrlno": "",
             "loadno": $("#EwtsConext_tdLoad").val(),
             "opdate": $(".date1").val(),
             "loadid": $(".hidloadid").val(),
             "outid":$(".hidoutid").val(),
             "loadnum": $(".spanload").html()

         }
         $.ajax({
             type: "post",
             url: "/ashx/Tally.ashx?isab=a",
             data: jston,
             success: function (msg) {
                 var obj = eval('(' + msg + ')');
                 var tr = "";
                 tr += "<td>" + ($('#tab_4 tr').length - 1) + "</td>";
                 tr += "<td>" + obj[0].goodscode + "</td>";
                 tr += "<td>" + obj[0].goosdname + "</td>";
                 tr += "<td class='td_ta_nt'>" + obj[0].nt + "</td>";
                 tr += "<td>" + obj[0].boxcount + "</td>";
                 tr += "<td>" + obj[0].qty + "</td>";
                 tr += "<td>" + obj[0].rubbishcode + "</td>";
                 tr += "<td>" + obj[0].rubbishname + "</td>";
                 tr += "<td>" + obj[0].kupono + "</td>";
                 tr += "<td>" + obj[0].classify + "</td>";
                 tr += "<td>" + obj[0].boxno + "</td>";
                 tr += "<td>" + obj[0].remark + "</td>";
                 tr += "<td>";
                 tr += " <a href=\"javascript:void(0)\" onclick=\"editData(this,'a',";
                 tr += obj[0].id;
                 tr += ")\">编辑</a>";
                 tr += "<a onclick=\"javascript:delData(this,'a',";
                 tr += obj[0].id + ")\">删除</a>";

                 tr += "</td>";
                 $(".hidtallyid").val(obj[1].id);
  
                 //document.cookie = "tallyid=" + obj[1].id + ";path=/";
                 $(".txttallyno").val(obj[1].tallyno);
                 if (obj[0] != null && $("#hidaid").val() != "0") {
                     $("#tr_aftid").html(tr);
                     $("#hidaid").val("0");
                     $("#tr_aftid").css("color", "black");
                 }
                 else {
                     tr = "<tr>" + tr + "</tr>";
                     $("#tab_4").append(tr);
                 }
                 comWeihtSurplus_a();   //余下重量更新
                 $("#txtgc_1").val("");
                 $("#txtgn_1").val("");
                 $("#txtnt_1").val("");
                 $("#txtboxcount_1").val("");
                 $("#txtqty_1").val("");
                 $("#txtrubc_1").val("");
                 $("#txtrubt_1").val("");
                 $("#txtldh_1").val("");
                 $("#txtboxno_1").val("");
                 $("#txtremark_1").val("");
                 $("tr:even").addClass("even");
                 //                $("tr").css("background", "#f6f6f6");
                 //                $("tr:even").css("background", "#e9e9e9");
                 $("#btnIn1").val("插入");
             }
         });
     }


     //验证处理前的物品编码，废物编码
     function checkInsertFirst() {
         $.ajax({
             type: "post",
             url: "/ashx/IsGoodCode.ashx",
             data: { goodCodeid: $("#txtgc_0").val(), isHw: $("#txtrubc_0").val(), sid: "", checkStatus: "0" },
             success: function (msg) {
                 switch (msg) {
                     case "-1":
                         alert("不能成功调用！");
                         break;
                     case "0":
                         insertFirst(); //调用插入函数
                         break;
                     case "1":
                         alert("物品编码不合法！");
                         break;
                     case "2":
                         alert("废物编码不合法！");
                         break;
                     default:
                         alert("不正常数据！");
                         break;
                 }
             }
         });
     }


     //验证处理后的物品编码，废物编码
     function checkInsertEnd() {
         $.ajax({
             type: "post",
             url: "/ashx/IsGoodCode.ashx?",
             data: { goodCodeid: $("#txtgc_1").val(), isHw: $("#txtrubc_1").val(), sid: "", checkStatus: "0" },
             success: function (msg) {
                 switch (msg) {
                     case "-1":
                         alert("不能成功调用！");
                         break;
                     case "0":
                         InsertEnd(); //调用插入函数
                         break;
                     case "1":
                         alert("物品编码不合法！");
                         break;
                     case "2":
                         alert("废物编码不合法！");
                         break;
                     default:
                         alert("不正常数据！");
                         break;
                 }
             }
         });
     }


     //验证第一个插入
     function checkInputOne() {
         var strErr = "";
         if ($("#txtgc_0").val() == "") strErr += "物品编码不能为空！\n";
         if ($("#txtgn_0").val() == "") strErr += "物品名称不能为空！\n";
         if ($("#txtnt_0").val() == "" || isNaN($("#txtnt_0").val())) strErr += "净重必须输入数字，且不为空！\n";
         if ($("#txtqty_0").val() == "" || isNaN($("#txtqty_0").val())) strErr += "数量必须输入数字，且不为空！\n";
         //        if ($("#txtrubc_0").val() == "") strErr += "废物编码不能为空！\n";
         //        if ($("#txtrubt_0").val() == "") strErr += "废物类型不能为空！\n";
         //        if ($("#txtboxno_0").val() == "") strErr += "箱号不能为空！\n";
         return strErr;
     }

     //验证第二个插入
     function checkInputTwo() {
         var strErr2 = "";
         if ($("#txtgc_1").val() == "") strErr2 += "物品编码不能为空！\n";
         if ($("#txtgn_1").val() == "") strErr2 += "物品名称不能为空！\n";
         if ($("#txtnt_1").val() == "" || isNaN($("#txtnt_1").val())) strErr2 += "净重必须输入数字，且不为空！\n";
         if ($("#txtboxcount_1").val() == "" || isNaN($("#txtboxcount_1").val())) strErr2 += "箱数必须输入数字，且不能为空！\n";
         if ($("#txtqty_1").val() == "" || isNaN($("#txtqty_1").val())) strErr2 += "数量必须输入数字，且不为空！\n";

         return strErr2;
     }


     //自动排序
     $(function () {
         LoadId();
     });

     function LoadId() {
         //$('table tr:not(:first)').remove();
         var len1 = $('#tab_1 tr').length;

         for (var i = 1; i < len1; i++) {
             $('#tab_1 tr:eq(' + i + ') td:first').text(i);
         }
         var len2 = $('#tab_2 tr').length;

         for (i = 1; i < len2; i++) {
             $('#tab_2 tr:eq(' + i + ') td:first').text(i);
         }
     }
 });
